/**
 * Created by Administrator on 2016/5/27.
 */
require(["jquery", "log", "rnetworking", "thisPlugins","page","pageMedia","util","utils"], function ($,log, rnetworking, plugins, page, pageMedia, util, utils) {
    $(function(){
        plugins.isLogined();
        var loginUser=plugins.getLoginUser();
        var roleId=loginUser.role_id;
        var powerMenuUser=rnetworking.create();
        powerMenuUser.addParams("params",getRoleId(roleId));
        powerMenuUser.ajax(utils.getIP()+"powerMenu/getPowerMenuUser",function(data){
            if(data.success){
                var html="<li class='nav-header'>"+
                    "<div class='dropdown profile-element'>"+
                    "<span><img alt='image' class='img-circle' src='media/img/profile_small.jpg' /></span>"+
                    "<a data-toggle='dropdown' class='dropdown-toggle' href='#'>"+
                    "<span class='clear'>"+
                    "<span class='block m-t-xs'><strong class='font-bold' id='loginName'></strong></span>"+
                    "<span class='text-muted text-xs block' id='roleName'><b class='caret'></b></span>"+
                    "</span>"+
                    "</a>"+
                    "<ul class='dropdown-menu animated fadeInRight m-t-xs'>"+
                    "<li><a class='J_menuItem' href='form_avatar.html'>修改密码</a>"+
                    "</li>"+
                    "<li class='divider'></li>"+
                    "<li><a href='#' onclick='exit()'>安全退出</a>"+
                    "</li>"+
                    "</ul>"+
                    "</div>"+
                    "</li>"+
                    "<li>"+
                    "<a href='main.html'><i class='fa fa-home'></i> <span class='nav-label'>主页</span></a>"+
                    "</li>";
                $.each(data.rows,function(i,val){
                    html+="<li>"+
                        "<a href='#'><i class='fa fa-cog'></i> <span class='nav-label'>"+val.menu_name+"</span><span class='fa arrow'></span></a>"+
                        "<ul class='nav nav-second-level'>";
                    if(data.result.length > 0){
                        $.each(data.result,function(j,obj){
                            if(val.id == obj.parent_id) {
                                html += "<li><a class='J_menuItem' href='" + obj.url + "'>"+obj.menu_name+"</a></li>" ;
                            }
                        })
                    }
                    html+="</ul></li>";
                })
                $("#side-menu").html(html);
                $("#loginName").html(loginUser.login_name);
                $("#roleName").html(getRoleName(roleId));
            }
        })

    })
    exit=function(){
        plugins.removeLoginUser();
        window.location.href="login.html";
    }
    function getRoleName(roleId){
        var roleName="";
        $.ajax({
            url:utils.getIP()+"powerRole/getPowerRoles?id="+roleId,
            async:false,
            success:function(data){
                if(data.success){
                    roleName=data.rows[0].role_name;
                }
            }
        })
        return roleName;
    }

    function getRoleId(roleId){
        var menuId="";
        $.ajax({
            url:utils.getIP()+"powerRoleMenu/getPowerRoleMenus?roleId="+roleId,
            async:false,
            success:function(data){
                if(data.success) {
                    for (var i=0;i<data.rows.length;i++) {
                        menuId += data.rows[i].menu_id + ","
                    }
                }
            }
        })
        menuId=menuId.substring(0,menuId.lastIndexOf(","));
        return menuId;
    }


})
